package masterlink.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import masterlink.bean.StockBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tw.fatsung.db.DbManager;

public class StockDAO implements Serializable {
	private static final Logger log=LoggerFactory.getLogger(StockDAO.class);
	private static final long serialVersionUID=1L;
	private static final String sqlq="select * from quote_stock where date=? and stock_id=? order by time";
	
	public List<StockBean> find(int date,String stock_id) throws Exception{
		log.trace("find date : {} , and stock_id : {}",date,stock_id);
		List<StockBean> list = new ArrayList<StockBean>();
		Connection conn = null;
		PreparedStatement pstmt =null;
		ResultSet rs=null;
		try{
			conn=DbManager.getConnection();
			pstmt=conn.prepareStatement(sqlq);
			pstmt.setInt(1, date);
			pstmt.setString(2,stock_id);
			rs=pstmt.executeQuery();
			while(rs.next()){
				StockBean sb = new StockBean();
				sb.setDate(rs.getInt("date"));
				sb.setStock_id(rs.getString("stock_id"));
				sb.setTime(rs.getInt("time"));
				sb.setBprice(rs.getInt("bprice"));
				sb.setSprice(rs.getInt("sprice"));
				sb.setDprice(rs.getInt("dprice"));
				sb.setTqty(rs.getInt("tqty"));
				list.add(sb);
			}
		}catch (Exception e) {
			log.error("StockDAO查錯誤!",e);
			throw e;
		}finally{
			DbManager.close(rs);
			DbManager.close(pstmt);
			DbManager.close(conn);
		}
		log.debug("Found {} objects for stock_id {}",list.size(),stock_id);
		return list;
	}
}
